<?php

namespace app\common\model\order;

use app\common\model\Goods;
use app\common\model\ScoreLog;
use fast\Random;
use think\Model;


class Order extends Model
{





    // 表名
    protected $name = 'order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'order_type_text',
        'pay_type_text',
        'order_status_text',
        'taketime_text',
        'cleantime_text',
        'paytime_text',
        'sendtime_text',
        'createtime_text',
        'take_images_array',
        'clean_images_array',
    ];

    public function getTakeImagesArrayAttr($value, $data)
    {
        if ($data['take_images']) {
            $array = explode(',', $data['take_images']);
            foreach ($array as &$v) {
                $v = cdnurl($v, true);
            }
            return $array;
        }
        return [];
    }
    public function getCleanImagesArrayAttr($value, $data)
    {
        if ($data['clean_images']) {
            $array = explode(',', $data['clean_images']);
            foreach ($array as &$v) {
                $v = cdnurl($v, true);
            }
            return $array;
        }
        return [];
    }
    public function getOrderTypeList()
    {
        return ['1' => __('Order_type 1'), '2' => __('Order_type 2')];
    }

    public function getPayTypeList()
    {
        return ['1' => __('Pay_type 1'), '2' => __('Pay_type 2'), '3' => __('Pay_type 3'), '4' => __('Pay_type 4'), '5' => __('Pay_type 5')];
    }

    public function getOrderStatusList()
    {
        return ['0' => __('Order_status 0'), '10' => __('Order_status 10'), '15' => __('Order_status 15'), '20' => __('Order_status 20'), '30' => __('Order_status 30'), '40' => __('Order_status 40'), '50' => __('Order_status 50'), '60' => __('Order_status 60')];
    }


    public function getOrderTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_type']) ? $data['order_type'] : '');
        $list = $this->getOrderTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOrderStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
        $list = $this->getOrderStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTaketimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['taketime']) ? $data['taketime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getCleantimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cleantime']) ? $data['cleantime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSendtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sendtime']) ? $data['sendtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreatetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function setTaketimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCleantimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSendtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    public function shop()
    {
        return $this->belongsTo('app\common\model\User', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function take()
    {
        return $this->belongsTo('app\common\model\User', 'take_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function send()
    {
        return $this->belongsTo('app\common\model\User', 'send_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods()
    {
        return $this->belongsTo('app\common\model\Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function takeaddress()
    {
        return $this->belongsTo('app\common\model\user\Address', 'take_address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function address()
    {
        return $this->belongsTo('app\common\model\user\Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    static function pay_completion($order_sn, $transaction_id = '')
    {
        try {
            $order = Order::get(['orderid' => $order_sn, 'order_status' => '0']);
            if ($order) {
                $order->order_status = '10';
                $order->total_points = ceil($order->total_fee);
                $order->paytime = time();
                $order->take_code = Random::numeric(5);
                $order->transaction_id = $transaction_id;
                $order->save();
                $consumption_point = config('site.consumption_point');
                if ($consumption_point > 0 && in_array($order->pay_type, ['1', '2'])) {
                    ScoreLog::income($order->user_id, $consumption_point, 1, 101, $order->orderid);
                }
                $goods = Goods::get($order->goods_id);
                $goods->setInc('sales', $order->num);
            }
        } catch (\Exception $e) {
            exception($e->getMessage());
        }
    }
}
